<?php
class AccountsController extends AppController {
    var $name = 'Accounts';

    function index() {
            $this->Account->recursive = 0;
 /*       	$permit  = $this->Session->read('permit_property');
			if ($permit != null){
				$data = null;
				$new  = null;                                                        
				foreach ($permit as $key => $value) {
					$this->paginate = array(
								 'conditions' => array( 'Account.property_id' => $value["property_id"] )
							 );  
					$data[$key]  = $this->paginate();
				}   
				foreach ( $data as $key1 => $value1)
				{   
					$new[$key1] = $value1[0];
				}     
					$this->set('accounts', $new);
			}else{
              $this->Session->setFlash(__('You have nothing assigned', true));
        	  $this->redirect(array('controller' => 'users', 'action' => 'welcome'));
			}*/
			$this->set('accounts', $this->paginate());
    }

    function view($id = null) {
            if (!$id) {
                    $this->Session->setFlash(__('Invalid account', true));
                    $this->redirect(array('action' => 'index'));
            }
            $this->set('account', $this->Account->read(null, $id));
    }

    function add() {
            if (!empty($this->data)) {
                    $this->Account->create();
                    if ($this->Account->save($this->data)) {
                            $this->Session->setFlash(__('The account has been saved', true));
                            $this->redirect(array('action' => 'index'));
                    } else {
                            $this->Session->setFlash(__('The account could not be saved. Please, try again.', true));
                    }
            }
            $properties = $this->Account->Property->find('list');
            $this->set(compact('properties'));
    }

    function edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid account', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->Account->save($this->data)) {
                $this->Session->setFlash(__('The account has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The account could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Account->read(null, $id);
        }
        $properties = $this->Account->Property->find('list');
        $this->set(compact('properties'));
    }
}
?>
